package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.github.javafaker.Faker;

import java.util.Locale;
import java.util.Random;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.Danmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class MainActivity extends AppCompatActivity {
    private Button sendButton;
    private EditText editText;
    private LinearLayout sendLayout;
    private VideoView videoview;
    private RelativeLayout activity_main;
    private DanmakuView danmakuView;
    private boolean showDanmaku;
    private DanmakuContext danmakuContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        playVideo();
        initDanmaku();
    }

    private void playVideo() {
        String uri="android.resource://"+getPackageName()+"/"+R.raw.sun;
        if (uri !=null){
            videoview.setVideoURI(Uri.parse(uri));
            videoview.start();
        }else {
            videoview.getBackground().setAlpha(0);//将背景设置为透明
        }
    }
    //创建弹幕解析器
    private BaseDanmakuParser parser=new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };
    //初始化弹幕
    private void initDanmaku(){
        danmakuView.setCallback(new DrawHandler.Callback() {//设置回调函数
            @Override
            public void prepared() {
                showDanmaku=true;
                danmakuView.start();//开始弹幕
                generateDanmakus();//调用随机生成弹幕方法
            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });
        danmakuContext=DanmakuContext.create();
        danmakuView.enableDanmakuDrawingCache(true);//提升屏幕绘制效率
        danmakuView.prepare(parser,danmakuContext);//进行弹幕准备
        //为danmaku设置点击事件
        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendLayout.getVisibility()==View.GONE){
                    sendLayout.setVisibility(View.VISIBLE);//显示布局
                }else {
                    sendLayout.setVisibility(View.GONE);//隐藏布局
                }
            }
        });
        //为发送按钮设置点击事件
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=editText.getText().toString();
                if (!TextUtils.isEmpty(content)){
                    addDanmaku(content,true);//添加一条弹幕
                    editText.setText("");
                }
            }
        });
    }
    private void addDanmaku(String content,boolean border){
        //创建弹幕对象
        BaseDanmaku danmaku=danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text=content;
        danmaku.padding=6;
        danmaku.textSize=30;
        danmaku.textColor= Color.WHITE;//弹幕文字的颜色
        danmaku.setTime(danmakuView.getCurrentTime());
        if (border){
            danmaku.borderColor=Color.BLUE;//弹幕文字边框的颜色
        }
        danmakuView.addDanmaku(danmaku);//添加一条弹幕
    }
    //随机生成一些弹幕内容
    private void generateDanmakus(){
        Faker faker = new Faker(new Locale("zh-CN"));
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (showDanmaku){
                    int num=new Random().nextInt(300);
                    String content=" "+num;
                    addDanmaku(content,false);
                    try {
                        Thread.sleep(num);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    @Override
    protected void onPause() {
        super.onPause();
        if (danmakuView != null && danmakuView.isPrepared()) {
            danmakuView.pause();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (danmakuView != null && danmakuView.isPrepared() &&
                danmakuView.isPaused()) {
            danmakuView.resume();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        showDanmaku = false;
        if (danmakuView != null) {
            danmakuView.release();
            danmakuView = null;
        }
    }

    private void initView() {
        sendButton=(Button)findViewById(R.id.btn_send);
        editText=(EditText)findViewById(R.id.et_text);
        sendLayout=(LinearLayout)findViewById(R.id.ly_send);
        videoview=(VideoView)findViewById(R.id.videoview);
        activity_main=(RelativeLayout)findViewById(R.id.activity_main);
        danmakuView=(DanmakuView)findViewById(R.id.danmaku);

    }
}
